package com.rambo.baselib.util;

import ohos.agp.utils.Point;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.global.resource.ResourceManager;

import java.util.Optional;

public class DpUtil {
    public static int dp2px(ResourceManager resourceManager,float dp) {
        if(resourceManager==null)return (int)dp;

        return (int) (resourceManager.getDeviceCapability().screenDensity / 160 * dp);
    }
    public static int getScreenW(Context context) {
        DisplayManager instance = DisplayManager.getInstance();
        Optional<Display> defaultDisplay = instance.getDefaultDisplay(context);
        if(defaultDisplay==null||!defaultDisplay.isPresent())return 0;
        Point size=new Point(0,0);
        defaultDisplay.get().getSize(size);
        return (int)size.position[0];
    }
    public static int getScreenH(Context context) {
        DisplayManager instance = DisplayManager.getInstance();
        Optional<Display> defaultDisplay = instance.getDefaultDisplay(context);
        if(defaultDisplay==null||!defaultDisplay.isPresent())return 0;
        Point size=new Point(0,0);
        defaultDisplay.get().getSize(size);
        return (int)size.position[1];
    }
}
